package com.ruoyi.wms.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.wms.domain.WmsProcureItem;
import com.ruoyi.wms.mapper.WmsProcureItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsProcureHeadMapper;
import com.ruoyi.wms.domain.WmsProcureHead;
import com.ruoyi.wms.service.IWmsProcureHeadService;
import com.ruoyi.common.core.text.Convert;

/**
 * 采购申请Service业务层处理
 * 
 * @author yy
 * @date 2024-02-15
 */
@Service
public class WmsProcureHeadServiceImpl implements IWmsProcureHeadService 
{
    @Autowired
    private WmsProcureHeadMapper wmsProcureHeadMapper;
    @Autowired
    private WmsProcureItemMapper wmsProcureItemMapper;
    /**
     * 查询采购申请
     * 
     * @param id 采购申请主键
     * @return 采购申请
     */
    @Override
    public WmsProcureHead selectWmsProcureHeadById(Long id)
    {
        return wmsProcureHeadMapper.selectWmsProcureHeadById(id);
    }

    /**
     * 查询采购申请列表
     * 
     * @param wmsProcureHead 采购申请
     * @return 采购申请
     */
    @Override
    public List<WmsProcureHead> selectWmsProcureHeadList(WmsProcureHead wmsProcureHead)
    {
        return wmsProcureHeadMapper.selectWmsProcureHeadList(wmsProcureHead);
    }

    /**
     * 新增采购申请
     * 
     * @param wmsProcureHead 采购申请
     * @return 结果
     */
    @Override
    public int insertWmsProcureHead(WmsProcureHead wmsProcureHead)
    {
        wmsProcureHead.setCreateTime(DateUtils.getNowDate());
        wmsProcureHead.setDeptId(ShiroUtils.getSysUser().getDeptId());
        wmsProcureHead.setDeptName(ShiroUtils.getSysUser().getDept().getDeptName());
        wmsProcureHeadMapper.insertWmsProcureHead(wmsProcureHead);

        for(WmsProcureItem item:wmsProcureHead.getItemList()){
            item.setCreateBy(ShiroUtils.getSysUser().getUserName());
            item.setCreateTime(new Date());
            item.setHeadId(wmsProcureHead.getId());
            wmsProcureItemMapper.insertWmsProcureItem(item);
        }
        return 1;
    }

    /**
     * 修改采购申请
     * 
     * @param wmsProcureHead 采购申请
     * @return 结果
     */
    @Override
    public int updateWmsProcureHead(WmsProcureHead wmsProcureHead)
    {
        wmsProcureHead.setUpdateTime(DateUtils.getNowDate());
        wmsProcureHeadMapper.updateWmsProcureHead(wmsProcureHead);

        wmsProcureItemMapper.deleteWmsProcureItemById(wmsProcureHead.getId());
        for(WmsProcureItem item:wmsProcureHead.getItemList()){
            item.setCreateBy(ShiroUtils.getSysUser().getUserName());
            item.setCreateTime(new Date());
            item.setHeadId(wmsProcureHead.getId());
            wmsProcureItemMapper.insertWmsProcureItem(item);
        }
        return 1;
    }

    /**
     * 批量删除采购申请
     * 
     * @param ids 需要删除的采购申请主键
     * @return 结果
     */
    @Override
    public int deleteWmsProcureHeadByIds(String ids)
    {
        return wmsProcureHeadMapper.deleteWmsProcureHeadByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除采购申请信息
     * 
     * @param id 采购申请主键
     * @return 结果
     */
    @Override
    public int deleteWmsProcureHeadById(Long id)
    {
        return wmsProcureHeadMapper.deleteWmsProcureHeadById(id);
    }
}
